package com.vancouver.knows.ui.activity.userpost;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewAnimator;

import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;
import com.vancouver.knows.R;
import com.vancouver.knows.business.bean.UserPostInfo;
import com.vancouver.knows.business.task.mark.GetUserPostsTaskMark;
import com.vancouver.knows.business.task.mark.PostPrivacySettingTaskMark;
import com.vancouver.knows.ui.base.BaseActivity;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Chris Li on 2016-07-22.
 */
public class UserPostListsActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    public static final String VIEW_EXTRA = "view_extra";
    public static final String IS_DRAFT_EXTRA = "is_draft_extra";

    public static final int HISTORY_VIEW = 0;
    public static final int FOLLOWING_VIEW = 1;

    @Bind(R.id.toolbar_title)
    TextView mToolbarTitle;
    @Bind(R.id.view_animator)
    ViewAnimator mViewAnimator;
    @Bind(R.id.posted_tab)
    RadioButton mPostedTab;
    @Bind(R.id.watching_tab)
    RadioButton mWatchingTab;
    @Bind(R.id.tab_group)
    RadioGroup mTabGroup;

    private UserPostListsPullRefreshView mUserDraftsController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_post_lists);

        String userId = imContext.getSharedPrefManager().getUserId();

        if (getIntent() != null) {
            boolean isDraftView = getIntent().getBooleanExtra(IS_DRAFT_EXTRA, false);
            if (isDraftView) {
                mUserDraftsController = new UserPostListsPullRefreshView(this);
                mUserDraftsController.loadData(GetUserPostsTaskMark.PAGE_HISTORY, userId, UserPostInfo.DRAFT_STATUS);
                mUserDraftsController.setLayoutParams(new ViewAnimator.LayoutParams(ViewAnimator.LayoutParams.MATCH_PARENT, ViewAnimator.LayoutParams.MATCH_PARENT));
                mViewAnimator.addView(mUserDraftsController);

                mTabGroup.setVisibility(View.GONE);
                mToolbarTitle.setVisibility(View.VISIBLE);

            } else {
                UserPostListsPullRefreshView historyController = new UserPostListsPullRefreshView(this);
                historyController.loadData(GetUserPostsTaskMark.PAGE_HISTORY, userId, "");
                historyController.setLayoutParams(new ViewAnimator.LayoutParams(ViewAnimator.LayoutParams.MATCH_PARENT, ViewAnimator.LayoutParams.MATCH_PARENT));
                mViewAnimator.addView(historyController);

                FollowUserPostsPullRefreshView followingController = new FollowUserPostsPullRefreshView(this);
                followingController.loadData(GetUserPostsTaskMark.PAGE_FOLLOWING, userId, "");
                followingController.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                mViewAnimator.addView(followingController);

                int viewToShow = getIntent().getIntExtra(VIEW_EXTRA, HISTORY_VIEW);
                mViewAnimator.setDisplayedChild(viewToShow);

                if (viewToShow == HISTORY_VIEW) {
                    mPostedTab.setChecked(true);
                } else if (viewToShow == FOLLOWING_VIEW) {
                    mWatchingTab.setChecked(true);
                }

                mTabGroup.setOnCheckedChangeListener(this);
            }
        }
    }

    @OnClick(R.id.back_icon)
    public void onBackButtonClicked() {
        onBackPressed();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.posted_tab) {
            mViewAnimator.setDisplayedChild(HISTORY_VIEW);
        } else if (checkedId == R.id.watching_tab) {
            mViewAnimator.setDisplayedChild(FOLLOWING_VIEW);
        }
    }

    @Override
    protected void onDestroy() {
        imContext.getBusinessModule().getCacheManager().getUserPostHistoryDraftList().clear();
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CreateUserPostActivity.CREATE_POST_SUCCESS_REQUEST && resultCode == RESULT_OK) {
            if (mUserDraftsController != null) {
                mUserDraftsController.removeUserPostItem();
            }
        }
    }
}
